package com.wzh.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @program: mall
 * @description:
 * @author: Mr.Wang
 * @create: 2021-09-02 19:04
 **/
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {


    private Set<Integer>  set= new HashSet<Integer>();

    /**
      * @Description: 初始化方法
      * @Param: [constraintAnnotation]
      * @return: void
      * @Author: Mr.Wang
      * @Date: 2021/9/2
      * @Time: 19:06
      */      
      
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] values = constraintAnnotation.values();
        for (int value:values) {
            set.add(value);
        }

    }

    /**
      * @Description: 判断是否检验成功
      * @Param: [value 需要校验的值, context]
      * @return: boolean
      * @Author: Mr.Wang
      * @Date: 2021/9/2
      * @Time: 19:06
      */      
      
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {

        return set.contains(value);
    }



}
